using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;

public partial class frmServiceProviderEdt : BasePage
{
    SortedView serviceProvidersSorted;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ResetSortControls();
            SearchAndBind(int.Parse(tbPage.Text), tbSortDirection.Text, "");
        }

    }

    public void SearchAndBind(int _pageIndex, string sortDirection, string sortProperty)
    {
        AdminServices.ServiceProviderSearchRequest searchRequest = new AdminServices.ServiceProviderSearchRequest();
        if (Request["SPName"] != null)
            searchRequest.DisplayName = Request["SPName"];
        if (Request["SPType"] != null)
            searchRequest.ServiceProviderType = Request["SPType"];
        searchRequest.InPool = true;

        AdminServices.AdminServices proxy = new AdminServices.AdminServices();
        AdminServices.ServiceProvider[] serviceProviders = proxy.ServiceProviderSearch(CurrentPrincipal, searchRequest);
        serviceProvidersSorted = new SortedView(serviceProviders);

        if (sortProperty != "")
        {
            ListSortDirection lsd;
            if (sortDirection == "ASC")
                lsd = ListSortDirection.Ascending; 
            else
                lsd = ListSortDirection.Descending;
            serviceProvidersSorted.ApplySort(sortProperty, lsd);
        }

        Session.Add("serviceProvidersSorted", serviceProvidersSorted);

        gvServiceProviders.DataSource = serviceProvidersSorted;
        gvServiceProviders.PageIndex = _pageIndex;
        gvServiceProviders.DataBind();

    }

    protected void gvServiceProviders_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        SearchAndBind(e.NewPageIndex, tbSortDirection.Text, tbSortProperty.Text);
        tbPage.Text = e.NewPageIndex.ToString();
    }

    protected void gvServiceProviders_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (tbSortDirection.Text == "ASC" && tbSortProperty.Text == e.SortExpression)
            tbSortDirection.Text = "DESC";
        else
            tbSortDirection.Text = "ASC";

        tbSortProperty.Text = e.SortExpression;
        SearchAndBind(int.Parse(tbPage.Text), tbSortDirection.Text, e.SortExpression);
    }

    private void ResetSortControls()
    {
        tbPage.Text = "0";
        tbSortDirection.Text = "ASC";
        tbSortProperty.Text = "";
    }

    protected void btnAddSP_Click(object sender, EventArgs e)
    {
        Response.Redirect("ServiceProviderAdd.aspx");
    }

    protected void gvServiceProviders_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        AdminServices.AdminServices proxy = new AdminServices.AdminServices();
        serviceProvidersSorted = (SortedView)Session["serviceProvidersSorted"];

        //unlock all
        proxy.ServiceProviderUnlockAll(CurrentPrincipal);
        
        switch (e.CommandName)
        {
            case "EditSP" :
                Response.Redirect("ServiceProviderAdd.aspx?SPID=" + gvServiceProviders.Rows[Convert.ToInt32(e.CommandArgument)].Cells[1].Text);
                break;
            case "DeleteSP" :
                //lock record
                proxy.ServiceProviderLock(CurrentPrincipal, gvServiceProviders.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Text); //((AdminServices.ServiceProvider)serviceProvidersSorted[int.Parse(e.CommandArgument.ToString())]).ServiceProviderID);
                //delete record
                proxy.ServiceProviderDelete(CurrentPrincipal, gvServiceProviders.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Text); //((AdminServices.ServiceProvider)serviceProvidersSorted[int.Parse(e.CommandArgument.ToString())]).ServiceProviderID);
                //unlock all
                proxy.ServiceProviderUnlockAll(CurrentPrincipal);
                break;
            case "CancelSP" :
                //unlock all
                proxy.ServiceProviderUnlockAll(CurrentPrincipal);

                gvServiceProviders.EditIndex = -1;
                break;
        }
        SearchAndBind(int.Parse(tbPage.Text), tbSortDirection.Text, "");
    }

    public string setServiceProviderTypeCode(object objServiceProviderType)
    {
        AdminServices.ServiceProviderType serviceProviderType = (AdminServices.ServiceProviderType)objServiceProviderType;
        return serviceProviderType.ServiceProviderTypeCode;
    }
}
